import {Sprite, Vector3} from "three";

export class Utils {
    public static getSpriteBBox(position: Vector3, sprite: Sprite, r: number): Array<number> {
        let minX = position.x - sprite.userData.rectWidth * r / 2;
        let maxX = position.x + sprite.userData.rectWidth * r / 2;
        let minY = position.y - sprite.userData.rectHeight * r / 2;
        let maxY = position.y + sprite.userData.rectHeight * r / 2;

        return [minX, maxX, minY, maxY];
    }

    public static intersects(bbox1: Array<number>, bbox2: Array<number>) {
        return bbox1[0] < bbox2[1] && bbox1[1] > bbox2[0]
            && bbox1[2] < bbox2[3] && bbox1[3] > bbox2[2]
    }

    public static checkHit(sprites: Array<Sprite>, r: number) {
        for (let i = 0; i < sprites.length; i++) {
            let sprite = sprites[i];

            let bbox1 = Utils.getSpriteBBox(sprite.position, sprite, r);

            for (let j = i + 1; j < sprites.length; j++) {
                let sprite2 = sprites[j];

                if (sprite2.visible === false) {
                    continue
                }

                let bbox2 = Utils.getSpriteBBox(sprite2.position, sprite2, r);

                let intersects = Utils.intersects(bbox1, bbox2);

                if (intersects === true) {
                    sprite.visible = false;
                    break;
                }
            }
        }
    }

    /**
     * 放大,检查被隐藏的sprite
     * @param {Array<Sprite>} sprites
     * @param r
     * @param allSprite
     */
    public static checkInHit(sprites: Array<Sprite>, r: number, allSprite: Array<Sprite>) {
        for (let i = 0; i < sprites.length; i++) {
            let sprite = sprites[i];

            let bbox1 = Utils.getSpriteBBox(sprite.position, sprite, r);

            let isBreak = false;

            for (let j = 0; j < allSprite.length; j++) {
                let sprite2 = allSprite[j];

                if (sprite2 === sprite || sprite2.visible === false) {
                    continue
                }

                let bbox2 = Utils.getSpriteBBox(sprite2.position, sprite2, r);

                let intersects = Utils.intersects(bbox1, bbox2);

                if (intersects === true) {
                    isBreak = true;
                    break;
                }
            }

            if (isBreak === false) {
                sprite.visible = true
            }
        }
    }

    /**
     * 缩小
     * @param {Array<Sprite>} sprites
     * @param r
     */
    public static checkOutHit(sprites: Array<Sprite>, r: number) {
        for (let i = 0; i < sprites.length; i++) {
            let sprite = sprites[i];

            let bbox1 = Utils.getSpriteBBox(sprite.position, sprite, r);

            for (let j = i + 1; j < sprites.length; j++) {
                let sprite2 = sprites[j];

                if (sprite2.visible === false) {
                    continue
                }

                let bbox2 = Utils.getSpriteBBox(sprite2.position, sprite2, r);

                let intersects = Utils.intersects(bbox1, bbox2);

                if (intersects === true) {
                    sprite.visible = false;
                    break;
                }
            }
        }
    }
}
